﻿using Avalonia.Controls;
using Avalonia.Media.Imaging;
using MyTodo.Common;
using MyTodo.Common.Models;
using MyTodo.Extensions;
using MyTodo.Views;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyTodo.ViewModels
{
    public class MainViewModel : NavigationViewModel, IConfigureService
    {
        #region 属性

        private string userName;
        public string UserName
        {
            get { return userName; }
            set { userName = value; RaisePropertyChanged(); }
        }
        public DelegateCommand LogoutCommand { get; set; }
        public DelegateCommand CmdWaiting { get; set; }
        public Bitmap? ImageUser { get; } = ImageHelper.LoadFromResource(new("avares://MyTodo/Images/user.jpg"));

        public DelegateCommand<MenuBar> NavigateCommand { get; private set; }
        public DelegateCommand GoBackCommand { get; private set; }
        public DelegateCommand GoForwardCommand { get; private set; }

        private ObservableCollection<MenuBar> menuBars;
        private readonly IRegionManager _regionManager;
        private readonly IContainerProvider containerProvider;
        private IRegionNavigationJournal journal;

        public ObservableCollection<MenuBar> MenuBars
        {
            get { return menuBars; }
            set { menuBars = value; RaisePropertyChanged(); }
        }

        private MenuBar _selectedMenu;
        /// <summary>
        /// NavigationDrawer.LeftDrawerContent中ListBox的SelectedItem绑定此属性，实现左侧导航菜单的选中
        /// </summary>
        public MenuBar SelectedMenu
        {
            get => _selectedMenu;
            set
            {
                SetProperty(ref _selectedMenu, value);
                if (value != null)
                {
                    NavigateCommand.Execute(value);
                }
            }
        }

        #endregion


        public MainViewModel(IRegionManager regionManager, IContainerProvider containerProvider) : base(containerProvider)
        {
            MenuBars = new ObservableCollection<MenuBar>();
            this._regionManager = regionManager;
            this.containerProvider = containerProvider;

            NavigateCommand = new DelegateCommand<MenuBar>(Navigate);

            GoBackCommand = new DelegateCommand(() =>
            {
                if (journal != null && journal.CanGoBack)
                    journal.GoBack();
            });
            GoForwardCommand = new DelegateCommand(() =>
            {
                if (journal != null && journal.CanGoForward)
                    journal.GoForward();
            });

            LogoutCommand = new DelegateCommand(() =>
            {
                App.Logout(containerProvider);
            });

            CmdWaiting = new DelegateCommand(() => 
            {

            });

            Configure(); //初始化
        }

        private void Navigate(MenuBar obj)
        {
            if (obj == null || string.IsNullOrWhiteSpace(obj.NameSpace))
                return;
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, obj.NameSpace, back =>
            {
                journal = back.Context.NavigationService.Journal;
            });
        }
          
        


        #region 导航类命令；拟废弃的方法

        public DelegateCommand CmdIndex => new(() =>
        {
            //// _journal.Clear();
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(IndexView));
        });

        public DelegateCommand CmdTodo => new(() =>
        {
            //// _journal.Clear();
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(ToDoView));
        });



        public DelegateCommand CmdMemo => new(() =>
        {
            //// _journal.Clear();
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(MemoView));
        });


        public DelegateCommand CmdOther => new(() =>
        {
            //// _journal.Clear();
            //_regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(ProgressView));

        });

 
        public DelegateCommand CmdSettings => new(() => _regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(SettingsView)));


        #endregion





        void CreateMenuBar()
        {
            MenuBars.Add(new MenuBar() { Icon = "Home", NameSpace = "IndexView", Title = "首页" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookOutline", NameSpace = "ToDoView", Title = "待办事项" });
            MenuBars.Add(new MenuBar() { Icon = "NotebookPlus", NameSpace = "MemoView", Title = "备忘录" });
            MenuBars.Add(new MenuBar() { Icon = "Cog", NameSpace = "SettingsView", Title = "设置" });
        }

        public void Configure()
        {
            UserName = AppSession.UserName;
            CreateMenuBar();
        
            //// _journal.Clear();
            _regionManager.RequestNavigate(PrismManager.MainViewRegionName, nameof(IndexView));

        }
    }
}
